//
//  GoodsDetailViewController.m
//  Mahjong
//
//  Created by Chen on 16/9/17.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "GoodsDetailViewController.h"
#import "GoodsDetailHeaderView.h"
#import "SDCycleScrollView.h"
#import "EvaluateTableViewCell.h"
#import "GoodsDetailButtomTableViewCell.h"
#import "EvaluateDetailViewController.h"
#import "AddShopCarView.h"
#import "EnsureListViewController.h"
#import "BrandDetailViewController.h"
#import "ChooseGoodsPropertyViewController.h"
#import "ShoppingCartViewController.h"
#import "AppTools.h"
static NSString *identifierForGoodsDetailButtomTableViewCell = @"GoodsDetailButtomTableViewCell";
static NSString *identifierForEvaluateTableViewCell = @"EvaluateTableViewCell";

@interface GoodsDetailViewController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

// 收藏naviItem
@property (nonatomic , strong) UIBarButtonItem *collectionItem;

@property (nonatomic , strong) UIView *headerView;

@property (nonatomic , strong) SDCycleScrollView *cycleView;

@property (nonatomic , strong) GoodsDetailHeaderView *detailHeaderView;

@property (nonatomic , strong) UITableView *tableView;

@property (nonatomic , strong) UIView *buttomView;

@end

@implementation GoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBarHidden = YES;

//    [self createNaviBarItems];
    
    [self createHeaderView];
    
    [self createTableView];
    
    [self createButtomView];
    [self addNav];
    [self create];
}
-(void)create{
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=GetGiftDetail&GiftId=%ld&ShopId=%ld",self.model.giftdetailId,self.model.shopid] success:^(id result) {
        NSLog(@"%@",result);

        
    } error:^(NSError *error) {
        
        
    }];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBarHidden = NO;
}
// 处理导航栏
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
-(void)addNav{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@" nav_but_back_xq@2x"] forState:UIControlStateNormal];
    backBtn.backgroundColor = [UIColor clearColor];
//    serviceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    backBtn.frame = CGRectMake(30, 30, 25, 25);
    [self.view addSubview:backBtn];
    
    UIButton *colleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [colleBtn setImage:[UIImage imageNamed:@"nav_but_collect@2x"] forState:UIControlStateNormal];
    [colleBtn setImage:[UIImage imageNamed:@"nav_but_collect_pre@2x"] forState:UIControlStateSelected];
    colleBtn.backgroundColor = [UIColor clearColor];
    //    serviceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [colleBtn addTarget:self action:@selector(collectionAction:) forControlEvents:UIControlEventTouchUpInside];
    colleBtn.frame = CGRectMake(kUIScreenWidth- 100, 30, 25, 25);
    [self.view addSubview:colleBtn];
    
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setImage:[UIImage imageNamed:@"nav_but_share@2x"] forState:UIControlStateNormal];
    shareBtn.backgroundColor = [UIColor clearColor];
    //    serviceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    shareBtn.frame = CGRectMake(kUIScreenWidth-50, 30, 25, 25);
    [self.view addSubview:shareBtn];
    
   
    

}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shareAction{
    // 这里需要继承分享的SDK 然后执行分享操作
}

- (void)collectionAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    // 收藏要注意 盘点收藏状态修改Item的图片
    // 这里已经设成了属性
    [AppTools createAFN:[NSString stringWithFormat:@"http://api03.m0571.com/WebJson.aspx?book=InsertCollection&giftid=%ld&shopid=%ld&userid=%@",self.model.giftdetailId,self.model.shopid, USERID] success:^(id result) {
        NSLog(@"%@",result);
        
    } error:^(NSError *error) {
        
        
    }];
}

- (void)createHeaderView{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight/5*2+130)];
    
    self.cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight/5*2) delegate:self placeholderImage:[UIImage imageNamed:@"lunbo1.png"]];
    self.cycleView.localizationImageNamesGroup = @[@"lunbo1.png", @"lunbo.png"]; //本地数组
    //网络数据
    //    cycleScrollView.imageURLStringsGroup = @[@"http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40f507b2e99aa64034f78f01930.jpg", @"http://b.hiphotos.baidu.com/zhidao/pic/item/4b90f603738da9770889666fb151f8198718e3d4.jpg"];
    self.cycleView.backgroundColor =[ UIColor clearColor];
    self.cycleView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
    self.cycleView.showPageControl = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.headerView addSubview:self.cycleView];
    // 轮播时间间隔，默认1.0秒，可自定义
    self.cycleView.autoScrollTimeInterval = 4.0;
    
    self.detailHeaderView = [GoodsDetailHeaderView initWithGoodsDetailHeaderView];
    self.detailHeaderView.frame = CGRectMake(0, kUIScreenHeight/5*2, kUIScreenWidth, 130);
    [self.headerView addSubview:self.detailHeaderView];
}

#pragma mark -- SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"点击了第%ld个图片", index);
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    NSLog(@"图片滚动回调%ld", index);
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - kTabbarHeight) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:identifierForEvaluateTableViewCell bundle:nil] forCellReuseIdentifier:identifierForEvaluateTableViewCell];
    [self.tableView registerNib:[UINib nibWithNibName:identifierForGoodsDetailButtomTableViewCell bundle:nil] forCellReuseIdentifier:identifierForGoodsDetailButtomTableViewCell];
    [self.view addSubview:self.tableView];
}

#pragma mark - TableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForEvaluateTableViewCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    } else {
        GoodsDetailButtomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierForGoodsDetailButtomTableViewCell forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 50;
    } else {
        return 160;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        return 20;
    }
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        EvaluateDetailViewController *evaluateDetailVC = [[EvaluateDetailViewController alloc] init];
        evaluateDetailVC.shopId = self.model.shopid;
        evaluateDetailVC.giftId = self.model.giftdetailId;
//        NSLog(@"%ld %ld",self.model.shopid,self.model.giftdetailId);
        [self.navigationController pushViewController:evaluateDetailVC animated:YES];
    }
    if (indexPath.section == 1) {
        BrandDetailViewController *evaluateDetailVC = [[BrandDetailViewController alloc] init];
        [self.navigationController pushViewController:evaluateDetailVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //设置separatorInset(iOS7之后)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //设置separatorInset(iOS7之后)
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    //设置layoutMargins(iOS8之后)
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)createButtomView{
    self.buttomView = [[UIView alloc] initWithFrame:CGRectMake(0, kUIScreenHeight - kTabbarHeight, kUIScreenWidth, kTabbarHeight)];
    
    UIButton *service = [UIButton buttonWithType:(UIButtonTypeCustom)];
    service.frame = CGRectMake((15*SCREEN_WIDTH)/375, 0, (30*SCREEN_WIDTH)/375, kTabbarHeight - 15);
//    [service setTitle:@"客服" forState:(UIControlStateNormal)];
    [service setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [service setImage:[UIImage imageNamed:@"tab_but_custum"] forState:(UIControlStateNormal)];
    [service addTarget:self action:@selector(serviceAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    [service layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyleTop) imageTitleSpace:0];
//    service.titleLabel.font = [UIFont systemFontOfSize:13];
//    service.titleLabel.textAlignment = NSTextAlignmentCenter;
    //[service setImageEdgeInsets:UIEdgeInsetsMake(-40, -40, -40, -40)];
    //[service setTitleEdgeInsets:UIEdgeInsetsMake(-30, -30, -30, -30)];
    [self.buttomView addSubview:service];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((10*SCREEN_WIDTH)/375, kTabbarHeight-15, (40*SCREEN_WIDTH)/375, 13)];
    label.text = @"客服";
    label.textAlignment = NSTextAlignmentCenter;
    [label setFont:[UIFont systemFontOfSize:(13*SCREEN_WIDTH)/375]];
    [self.buttomView addSubview:label];
    
    
    
    UIButton *shopCar = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shopCar.frame = CGRectMake((90*SCREEN_WIDTH)/375, 0, (30*SCREEN_WIDTH)/375, kTabbarHeight - 15);
//    [shopCar setTitle:@"购物车" forState:(UIControlStateNormal)];
    [shopCar setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [shopCar setImage:[UIImage imageNamed:@"tab_but_shopcart"] forState:(UIControlStateNormal)];
    [shopCar addTarget:self action:@selector(shopCarAction:) forControlEvents:(UIControlEventTouchUpInside)];
//    [shopCar layoutButtonWithEdgeInsetsStyle:(MKButtonEdgeInsetsStyleTop) imageTitleSpace:0];
//    shopCar.titleLabel.font = [UIFont systemFontOfSize:13];
//    shopCar.titleLabel.textAlignment = NSTextAlignmentCenter;
    //[shopCar setTitleEdgeInsets:UIEdgeInsetsMake(-20, -20, -20, -20)];
    //[shopCar setImageEdgeInsets:UIEdgeInsetsMake(-20, -20, -20, -20)];
    [self.buttomView addSubview:shopCar];
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake((90*SCREEN_WIDTH)/375, kTabbarHeight-15, (40*SCREEN_WIDTH)/375, 13)];
    la.text = @"购物车";
    la.textAlignment = NSTextAlignmentCenter;
    [la setFont:[UIFont systemFontOfSize:(13*SCREEN_WIDTH)/375]];
    [self.buttomView addSubview:la];
    
    UIButton *addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    addBtn.frame = CGRectMake(kUIScreenWidth - (210*SCREEN_WIDTH)/375, 7.5, (100*SCREEN_WIDTH)/375, kTabbarHeight - 15);
    [addBtn setTitle:@"加入购物车" forState:(UIControlStateNormal)];
    [addBtn.titleLabel setFont:[UIFont systemFontOfSize:(18*SCREEN_WIDTH)/375]];

    [addBtn setBackgroundColor:[UIColor redColor]];
    
    addBtn.layer.cornerRadius = 5;
    addBtn.clipsToBounds = YES;
    [self.buttomView addSubview:addBtn];
    [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *buyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buyBtn.frame = CGRectMake(kUIScreenWidth - (100*SCREEN_WIDTH)/375, 7.5, (80*SCREEN_WIDTH)/375, kTabbarHeight - 15);
    [buyBtn setTitle:@"立刻购买" forState:(UIControlStateNormal)];
    [buyBtn.titleLabel setFont:[UIFont systemFontOfSize:(18*SCREEN_WIDTH)/375]];
    [buyBtn setBackgroundColor:[UIColor redColor]];

    buyBtn.layer.cornerRadius = 5;
    buyBtn.clipsToBounds = YES;
    [self.buttomView addSubview:buyBtn];
    [buyBtn addTarget:self action:@selector(buyBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    [self.buttomView addSubview:lineView];
    
    [self.view addSubview:self.buttomView];
}

- (void)serviceAction:(UIButton *)btn{
    
}

- (void)shopCarAction:(UIButton *)btn{
//    AddShopCarView *addShopCarView = [[AddShopCarView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];
//    [self.view.window addSubview:addShopCarView];
    
    ShoppingCartViewController *good = [[ShoppingCartViewController alloc] init];
//    good.view.backgroundColor=[UIColor colorWithWhite:0 alpha:0.4];
//    good.modalPresentationStyle = UIModalPresentationOverFullScreen;
    good.type = 1;
    [self.navigationController pushViewController:good animated:YES];
    
//    [self.navigationController presentViewController:good animated:YES completion:^{
//        
//        
//    }];
}
- (void)addBtnAction:(UIButton *)btn{
    ChooseGoodsPropertyViewController *good = [[ChooseGoodsPropertyViewController alloc] init];
    good.shopId = self.model.shopid;
    good.orderId = self.model.giftdetailId;
    good.view.backgroundColor=[UIColor colorWithWhite:0 alpha:0.4];
    good.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [self.navigationController presentViewController:good animated:YES completion:^{
        
        
    }];
}
- (void)buyBtnAction:(UIButton *)btn{
    EnsureListViewController *vc = [[EnsureListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
